# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>
# Contributor: Peter Budai <peterbudai@hotmail.com>

_realname=hunspell
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
# Please rebuild enchant when updating this if needed, otherwise it may
# silently break spell checking in applications
pkgver=1.7.2
pkgrel=3
pkgdesc="Spell checker and morphological analyzer library and program (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://hunspell.github.io/"
# We omit MPL because it cannot be applied to the whole source, see COPYING
license=(GPL2+ LGPL2.1+)
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" 
             "${MINGW_PACKAGE_PREFIX}-autotools")
depends=(${MINGW_PACKAGE_PREFIX}-gcc-libs
         ${MINGW_PACKAGE_PREFIX}-gettext
         ${MINGW_PACKAGE_PREFIX}-ncurses
         ${MINGW_PACKAGE_PREFIX}-readline)
options=('!libtool' 'strip' '!debug')
optdepends=("perl: for ispellaff2myspell")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/hunspell/hunspell/archive/v${pkgver}.tar.gz"
        pathtools.c
        pathtools.h
        01-relocate.patch
        02-fix-link-with-pdcurses.patch)
noextract=("${_realname}-${pkgver}.tar.gz")
sha256sums=('69fa312d3586c988789266eaf7ffc9861d9f6396c31fc930a014d551b59bbd6e'
            '08209cbf1633fa92eae7e5d28f95f8df9d6184cc20fa878c99aec4709bb257fd'
            '965d3921ec4fdeec94a2718bc2c85ce5e1a00ea0e499330a554074a7ae15dfc6'
            '628ac7f44ae0e093f229e88316cc8fbfb027cdcbe2bc5cc28d75bc3c57413f7a'
            '260432a8f01525e83124c79e375cc9f0692b1a6c97773add4c69b8efe081cf1d')

prepare() {
  cd ${srcdir}
  tar zxf ${_realname}-${pkgver}.tar.gz || true

  test ! -d "${startdir}/../mingw-w64-pathtools" || {
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.c" "${srcdir}/pathtools.c" &&
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.h" "${srcdir}/pathtools.h"
  } || exit 1

  cd "${srcdir}/${_realname}-${pkgver}"
  cp -fHv "${srcdir}"/pathtools.c src/tools/pathtools.cxx
  cp -fHv "${srcdir}"/pathtools.h src/tools/

  cp -Rp README.md README
  patch -Np1 -i ${srcdir}/01-relocate.patch
  #patch -Np1 -i ${srcdir}/02-fix-link-with-pdcurses.patch
  autoreconf -fi
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  #CPPFLAGS+=" -I${MINGW_PREFIX}/include/ncurses"
  #CFLAGS+=" -I${MINGW_PREFIX}/include/ncurses"
  #CXXFLAGS+=" -I${MINGW_PREFIX}/include/ncurses"

  ../${_realname}-${pkgver}/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --disable-rpath \
    --with-ui \
    --with-readline

  MSYS2_ARG_CONV_EXCL="-DBINDIR=;-DDATA_DIR=" \
  make
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make check
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  MSYS2_ARG_CONV_EXCL="-DBINDIR=;-DDATA_DIR=" \
  make DESTDIR="${pkgdir}" install
  cp ${pkgdir}${MINGW_PREFIX}/lib/lib${_realname}-${pkgver%.*}.dll.a ${pkgdir}${MINGW_PREFIX}/lib/lib${_realname}.dll.a

  # Licenses
  cd "${srcdir}/${_realname}-${pkgver}"
  install -Dm644 COPYING      "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 COPYING.LESSER "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LESSER"
  install -Dm644 COPYING.MPL  "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.MPL"

}
